﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SweeperPrototype.Business.Fields;
using SweeperPrototype.Business.Keys;
using SweeperPrototype.Business.Plumbing;
using SweeperPrototype.Business.Plumbing.Requests;
using SweeperPrototype.GUI;
using SweeperPrototype.GUI.Behaviors.AttachedCommand;
using SweeperPrototype.GUI.EffectiveExposures;
using SweeperPrototype.GUI.ExposureMappings;
using SweeperPrototype.Objects;
using SweeperPrototype.Plumbing;

namespace SweeperPrototype
{

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private LinkEffectiveExposures link;
        public MainWindow()
        {
            InitializeComponent();
            BookCode code = new BookCode("PT600");
            PositionProvider provider = new PositionProvider(new PositionRequestManager(
                new MockPositionSource( code )));
            ExposureMapProvider exposure_map_provider = new ExposureMapProvider(
                new MockExposureMappingSource()
                );
            EffectiveExposureProvider eff_ex_provider = new EffectiveExposureProvider(
                provider, exposure_map_provider 
                );


            AppModel appvm = new AppModel();
            appvm.Positions = WirePositions(provider, code);
            NotifyList<ExposureMappingSubscription> mappings = new NotifyList<ExposureMappingSubscription>();
            appvm.Exposures = WireExposures( mappings );


            NotifyList<EffectiveExposure> eff_ex_list;
            link = new LinkEffectiveExposures(
                appvm.Positions.SelectedPositions,
                out eff_ex_list,
                eff_ex_provider
                );

            appvm.EffectiveExposures = WireEffectiveExposures(eff_ex_list);

            appvm.Positions.SelectedPositions.Addition
                += delegate(PositionSubscription addition, int index)
                       {
                           mappings.Add(
                                new ExposureMappingSubscription( addition.RIC,
                                    new EffectiveRIC(addition.RIC.Value) )
                                );               
                       };

            appvm.Positions.SelectedPositions.Removal 
                += delegate(PositionSubscription removal, int index)
                       {
                           mappings.Remove( index );
                       };
    
            DataContext = appvm;
        }

        private EffectiveExposureListViewModel WireEffectiveExposures(NotifyList<EffectiveExposure> eff_ex_list)
        {
            EffectiveExposureListViewModel vm = new EffectiveExposureListViewModel( eff_ex_list );
            return vm;
        }


        private ExposureMapListViewModel WireExposures(NotifyList<ExposureMappingSubscription> mappings)
        {
            ExposureMapListViewModel vm = new ExposureMapListViewModel( mappings );

            return vm;
        }


        public class AppModel
        {
            public PositionListViewModel Positions { get; set; }
            public ExposureMapListViewModel Exposures { get; set; }
            public EffectiveExposureListViewModel EffectiveExposures { get; set; }
        }

        public PositionListViewModel WirePositions( PositionProvider provider, BookCode code  )
        {
            NotifyList<PositionSubscription> positions = provider.Subscribe(code);
            PositionListViewModel list_view_model = new PositionListViewModel(positions);
            return list_view_model;
        }

    }


}
